<template>
	<view class="artwork" id="arc-content">
		<uni-nav-bar :fixed="true" left-icon="back" :border="false" backgroundColor="#f9f2ea" leftText="商品详情"
			:statusBar="true" @clickLeft="goBack"></uni-nav-bar>
		<view class="information">
			<view class="information-top">
				<swiper class="swiper" :indicator-dots="true" :autoplay="true" :interval="3000" :duration="1000">
					<swiper-item v-for="(item, index) in goodData.carousel" :key="index">
						<image class="img" :src="item.image" mode=""></image>
					</swiper-item>
				</swiper>
			</view>
			<view class="information-content">
				<view class="left">
					<view class="price">
						￥{{goodData.price}}
					</view>
					<view class="title">
						{{goodData.name}}
					</view>
				</view>
				<view class="right">
					<view class="collect-box" @click="collect(goodData.is_collect)">
						<image class="img" v-show="goodData.is_collect == false" src="../../static/collect.png" mode="">
						</image>
						<image class="img" v-show="goodData.is_collect == true" src="../../static/mine_collect.png"
							mode=""></image>
						<text>喜欢</text>
					</view>
					<button open-type="share" class="location">
						<image class="img" src="../../static/share.png" mode=""></image>
						<text>分享</text>
					</button>
				</view>
			</view>
		</view>
		<view class="detail">
			<view class="detail-top">
				图文详情
			</view>
			<view class="detail-box">
				<jyf-parser :html="goodData.detail"></jyf-parser>
			</view>
		</view>
		<recommend></recommend>
		<view class="btn-group flex">
			<view class="btn cart" @click="subscribeShow = true;buyType = 1;">
				加入购物车
			</view>
			<view class="btn buy" @click="subscribeShow = true;buyType = 2;">
				立即购买
			</view>
		</view>
		<view class="mark" v-show="subscribeShow">
			<view class="mark-bg" @click="subscribeShow = false">

			</view>
			<view class="mark-box">
				<image class="img" @click="subscribeShow = false" src="../../static/search_delete.png" mode="">
				</image>
				<view class="box-top flex">
					<view class="top-left">
						<image class="img" :src="buyData.thumb" mode=""></image>
					</view>
					<view class="top-center">
						<view class="price">
							￥{{buyData.price}}
						</view>
						<view class="">
							库存：{{buyData.stock}}
						</view>
						<view class="">
							{{buyData.spec_name}}
						</view>
					</view>
					<view class="top-right">

						<view class="number-box flex">
							<view class="reduce frame" @click="reduce">
								<image class="img" v-if="buyNumber>1" src="../../static/reduce.png" mode=""></image>
								<image class="img" v-else src="../../static/unreduce.png" mode=""></image>
							</view>
							<view class="number">
								{{buyNumber}}
							</view>
							<view class="add frame" @click="add">
								<image class="img" v-if="buyNumber<99999" src="../../static/add.png" mode=""></image>
								<image class="img" v-else src="../../static/unadd.png" mode=""></image>
							</view>
						</view>
					</view>
				</view>
				<view class="box-center">
					<view class="" v-for="(items, index) in specList" :key="index">
						<view class="type">
							{{items.name}}
						</view>
						<view class="flex name-box">
							<view class="name" :class="{'active': items.chooseId == item.id}"
								v-for="(item, idx) in items.value" :key="idx" @click="buyChoose(item, items)">
								{{item.name}}
							</view>
						</view>
					</view>
				</view>
				<view class="btn-group flex">
					<view class="btn confirm" @click="confirmBuy()">
						确认
					</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import {
		getGoodsInfo,
		getGoodsComment,
		addCollect,
		cancelCollect,
		addCart,
		accountOrder
	} from "@/api/public.js"
	import recommend from "@/components/recommend.vue"
	import jyfParser from '@/components/jyf-parser/jyf-parser.vue'
	export default {
		data() {
			return {
				commentList: [],
				goods: [],
				buyData: {},
				specList: [],
				id: "",
				goodData: {},
				commentPage: 1,
				commentTotal: 0,
				subscribeShow: false,
				buyNumber: 1,
				buyType: 0,
			}
		},
		methods: {
			goBack() {
				uni.navigateBack();
			},
			// 获取商品信息
			getInfo() {
				getGoodsInfo(this.id).then(res => {
					this.goodData = res.reData.info;
					console.log(this.goodData)
					this.goods = res.reData.goods;
					this.buyData = this.goods[0];
					this.specList = res.reData.spec;
					for (let i in this.specList) {
						this.specList[i].chooseId = this.specList[i].value[0].id
					}
				})
			},
			// 获取评价
			getComment() {
				getGoodsComment(this.id, this.commentPage).then(res => {
					this.commentTotal = res.reData.total;
					this.commentList = res.reData.data;
				})
			},
			// 锚点跳转到评价
			toCommentAddress() {
				uni.createSelectorQuery().select('#comment-content').boundingClientRect(data => { //目标位置的节点：类或者id
					uni.createSelectorQuery().select("#arc-content").boundingClientRect(res => { //最外层盒子的节点：类或者id
						uni.pageScrollTo({
							duration: 100, //过渡时间
							scrollTop: data.top - res.top, //到达距离顶部的top值
						})
					}).exec()
				}).exec();
			},
			// 选择分类
			buyChoose(item, items) {
				items.chooseId = item.id;
				let arr = [];
				for (let i in this.specList) {
					arr.push(this.specList[i].chooseId);
				}
				let str = arr.toString(",")
				for (let j in this.goods) {
					if (this.goods[j].spec_values == str) {
						this.buyData = this.goods[j];
					}
				}
				this.$forceUpdate();
			},
			// 收藏取消收藏
			collect(type) {
				if (type) {
					cancelCollect(this.id, 1).then(res => {
						if (res.result == 0) {
							this.goodData.is_collect = false;
							this.$forceUpdate();
						}
					})
				} else {
					addCollect(this.id, 1).then(res => {
						if (res.result == 0) {
							this.goodData.is_collect = true;
							this.$forceUpdate();
						}
					})
				}
			},
			confirmBuy() {
				if (this.buyType == 1) {
					this.addShoppingCart()
				} else if (this.buyType == 2) {
					this.bayNow();
				}
			},
			reduce() {
				if (this.buyNumber > 1) {
					this.buyNumber--;
				}
			},
			add() {
				if (this.buyData.stock == 0) {
					uni.showToast({
						title: "暂无库存",
						icon: "none"
					})
				} else if (this.buyNumber >= this.buyData.stock) {
					uni.showToast({
						title: "库存不足",
						icon: "none"
					})
				} else if (this.buyNumber < this.buyData.stock) {
					this.buyNumber++;
				}

			},
			addShoppingCart() {
				if (this.buyData.stock == 0) {
					uni.showToast({
						title: "暂无库存",
						icon: "none"
					})
					return false
				} else if (this.buyNumber >= this.buyData.stock) {
					uni.showToast({
						title: "库存不足",
						icon: "none"
					})
					return false
				}
				addCart(this.buyData.id, this.buyNumber, 1).then(res => {
					if (res.result == 0) {
						uni.showToast({
							title: "加入成功",
							icon: "success",
						})
					} else {
						uni.showToast({
							title: res.message,
							icon: "none",
						})
					}
				})
			},
			bayNow() {
				if (this.buyData.stock == 0 || this.buyData.stock < this.buyNumber) {
					uni.showToast({
						title: "库存不足",
						icon: "none"
					})
					return false
				}
				let arr = [{
					"goods_id": this.buyData.id,
					"goods_num": this.buyNumber,
					"goods_type": 1
				}];
				let str = JSON.stringify(arr);
				accountOrder(str).then(res => {
					if (res.result == 0) {
						uni.setStorageSync("accountsType", 1);
						uni.navigateTo({
							url: "../shoppingCart/confirmOrder?goods=" + str + "&orderType=1"
						})
					} else {
						uni.showToast({
							title: res.message,
							icon: "none",
						})
					}
				})
			},
		},
		onLoad(obj) {
			this.id = obj.id;
			this.getInfo();
			// this.getComment();
		},
		components: {
			recommend,
			jyfParser
		},
		// 分享配置
		onShareAppMessage(res) {
			return {
				title: this.goodData.name,  
				path:"pages/index/artWorkDetails?id="+this.goodData.id,
				imageUrl: this.goodData.thumb
			}
		},
		// 分享配置
		onShareTimeline(res) {
			return {
				title: this.goodData.name,  
				path:"pages/index/artWorkDetails?id="+this.goodData.id,
				imageUrl: this.goodData.thumb
			}
		},
	}
</script>

<style lang="scss" scoped>
	.artwork {
		padding-bottom: 120rpx;
	}

	.information {
		padding: 30rpx 30rpx 0;

		.information-top {
			position: relative;
			width: 690rpx;
			height: 690rpx;
			margin-bottom: 30rpx;

			.swiper {
				width: 100%;
				height: 100%;
			}

			.img {
				width: 100%;
				height: 100%;
			}

			.type {
				position: absolute;
				right: 20rpx;
				bottom: 20rpx;
				width: 80rpx;
				height: 36rpx;
				background-color: rgba(153, 153, 153, 0.5);
				border-radius: 8rpx;
				font-size: 22rpx;
				line-height: 36rpx;
				text-align: center;
				color: #fff;
			}
		}

		.information-content {
			display: flex;
			justify-content: space-between;
			align-items: center;
			background: #fffaf5;
			border-radius: 20rpx;
			overflow: hidden;
			padding: 20rpx 30rpx;
			box-shadow: 0px 2px 4px rgba(0, 0, 0, 0.11);

			.left {
				.price {
					font-size: 36rpx;
					line-height: 36rpx;
					font-weight: 700;
					color: rgba(216, 127, 129, 1);
					margin-bottom: 10rpx;
				}

				.title {
					font-size: 28rpx;
					line-height: 32rpx;
					color: rgba(140, 111, 111, 1);
					max-width: 360rpx;
				}
			}

			.right {
				display: flex;
				align-items: center;
				justify-content: space-between;

				.img {
					width: 24rpx;
					height: 24rpx;
					margin-right: 10rpx;
				}

				.collect-box {
					display: flex;
					align-items: center;

					text {
						font-size: 24rpx;
						color: rgba(128, 128, 128, 1);
					}
				}

				.location {
					padding: 0;
					border: none;
					font-size: 22rpx;
					line-height: 34rpx;
					color: #999;
					display: flex;
					align-items: center;
					height: 48rpx;
					margin-left: 40rpx;
					background: transparent;

					text {
						font-size: 24rpx;
						color: rgba(128, 128, 128, 1);
					}

					&::after {
						border: none;
					}
				}

				.collect {
					width: 120rpx;
					height: 72rpx;
					background-color: rgba(214, 142, 127, 1);
					border-radius: 20rpx;
					line-height: 72rpx;
					font-size: 32rpx;
					color: #fff;
					text-align: center;
				}
			}
		}
	}

	.detail {
		// background: #fff;
		margin-bottom: 30rpx;

		.detail-top {
			text-align: center;
			font-size: 28rpx;
			line-height: 52rpx;
			color: rgba(140, 111, 111, 1);
			padding: 30rpx;
		}

		.detail-box {
			padding: 20rpx 30rpx;
			background: #fffaf5;
		}

		/deep/ image {
			width: 100%;
		}
	}

	.btn-group {
		position: fixed;
		width: 100%;
		bottom: 0;
		height: 120rpx;
		overflow: hidden;
		display: flex;
		align-items: center;
		justify-content: space-around;
		background-color: rgba(255, 250, 245, 1);
		z-index: 10;

		.btn {
			height: 88rpx;
			font-size: 30rpx;
			line-height: 88rpx;
			color: #fff;
			text-align: center;
			box-shadow: 10rpx 10rpx 20rpx rgba(189, 185, 166, 0.5);

			&.cart {
				width: 220rpx;
				background-color: rgba(255, 250, 245, 1);
				border: 2rpx solid rgba(214, 142, 127, 1);

				border-radius: 50rpx;
				color: rgba(214, 142, 127, 1);
			}

			&.buy {
				width: 400rpx;
				background-color: rgba(214, 142, 127, 1);

				border-radius: 50rpx;
			}

			&.confirm {
				width: 220rpx;
				background-color: rgba(214, 142, 127, 1);

				border-radius: 50rpx;
			}
		}
	}

	.mark {
		position: fixed;
		top: 0;
		left: 0;
		width: 100%;
		height: 100%;
		z-index: 10;

		.mark-bg {
			width: 100%;
			height: 100%;
			background: rgba(0, 0, 0, 0.5);
		}

		.mark-box {
			position: absolute;
			background: #fffaf5;
			padding-bottom: 120rpx;
			left: 0;
			right: 0;
			bottom: 0;
			margin: 0 auto;
			border-radius: 20rpx 20rpx 0 0;

			>.img {
				width: 40rpx;
				height: 40rpx;
				position: absolute;
				right:20rpx;
				top:20rpx;
			}


			.box-top {
				padding:80rpx 30rpx 30rpx 30rpx;

				.top-left {
					margin-right: 20rpx;

					.img {
						width: 160rpx;
						height: 160rpx;
						border-radius: 10rpx;
					}
				}

				.top-center {
					flex-grow: 1;
					font-size: 28rpx;
					color: rgba(140, 111, 111, 1);
					line-height: 50rpx;

					.price {
						color: rgba(214, 142, 127, 1);

					}
				}

				.top-right {
					width: 240rpx;
					display: flex;
					margin-left: 20rpx;
					flex-wrap: wrap;
					justify-content: flex-end;


					.number-box {
						padding: 20rpx;

						.frame {
							width: 48rpx;
							height: 48rpx;

							.img {
								width: 100%;
								height: 100%;
							}
						}

						.number {
							margin: 0 30rpx;
							line-height: 46rpx;
							color: #333;
							font-size: 26rpx;
						}
					}
				}
			}

			.box-center {
				padding: 30rpx;
				background-color: rgba(249, 242, 234, 1);

				.type {
					color: #333;
					font-size: 28rpx;
					line-height: 36rpx;
					margin-bottom: 10rpx;
				}

				.name-box {
					flex-wrap: wrap;

					.name {
						padding: 0 20rpx;
						font-size: 26rpx;
						line-height: 44rpx;
						color: rgba(214, 142, 127, 1);
						background-color: rgba(255, 250, 245, 1);
						border: 2rpx solid rgba(214, 142, 127, 1);
						border-radius: 22rpx;
						margin-right: 20rpx;
						margin-bottom: 10rpx;

						&.active {
							color: rgba(255, 250, 245, 1);
							background-color: rgba(214, 142, 127, 1);
						}
					}
				}
			}
		}
	}
</style>
